package com.arpa.ntocc.basic.api.controller;


import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.controller.BaseController;
import com.arpa.ntocc.common.domain.dto.ResourceColDTO;
import com.arpa.ntocc.common.domain.entity.ResourceCol;
import com.arpa.ntocc.common.domain.vo.ResourceColVO;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.ntocc.common.service.IResourceColService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 菜单列表字段 Controller
 * </p>
 *
 * @author liuyj
 * @since 2019-11-25
 */
@Api(tags = "菜单列表字段")
@RestController
@RequestMapping(value="/resourceCol", produces = "application/json;charset=UTF-8")
@Log4j2
public class ResourceColController extends BaseController {

   @Autowired
   private IResourceColService  iResourceColService;

    /**
     * 获取 菜单列表字段 列表
     *
     * @param resourceColDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 菜单列表字段 列表", notes = "获取 菜单列表字段 列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @GetMapping()
    public Result list(ResourceColDTO resourceColDTO, HttpServletRequest request) {
        try {
            ResourceColVO resourceColVOSum = iResourceColService.queryListSum(resourceColDTO);
            List<ResourceColVO> resourceColVOList = iResourceColService.queryList(resourceColDTO);

            Page returnPage = new Page(resourceColDTO.getPageNum(),resourceColDTO.getPageSize());
            returnPage.setRecords(resourceColVOList);
            returnPage.setTotal(resourceColVOSum.getTotal());
            returnPage.setSumObject(resourceColVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加 菜单列表字段
     *
     * @param entity ResourceCol
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 菜单列表字段 ", notes = "添加 菜单列表字段 ")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
    @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping()
    public Result create(ResourceCol entity, HttpServletRequest request) {
        try {
            iResourceColService.save(entity);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
    * 获取菜单列表字段详情
    *
    * @param code
    * @param request
    * @return
    */
    @ApiOperation(value = "获取菜单列表字段详情", notes = "获取菜单列表字段详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "code", value = "菜单列表字段code", paramType = "path", dataType = "String")
    })
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result resourceCol(@PathVariable String code, HttpServletRequest request) {
        try {
            ResourceCol entity = iResourceColService.getOne(new QueryWrapper<ResourceCol>().lambda().eq(ResourceCol::getCode,code));
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改菜单列表字段
     *
     * @param entity 菜单列表字段
     * @param request
     * @return
     */
    @ApiOperation(value = "修改菜单列表字段", notes = "修改菜单列表字段")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping()
    public Result update(ResourceCol entity, HttpServletRequest request) {
        if(StringUtils.isBlank(entity.getCode())){
            return Result.error("code不能为空");
        }
        int updateNum = iResourceColService.updateByCode(entity);
        if(updateNum == 0){
            return Result.error("没有更新数据！code:"+entity.getCode());
        }else{
            return Result.ok("修改成功,更新"+updateNum+"条数据！");
        }
    }

    /**
     * 删除 菜单列表字段
     *
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "删除 菜单列表字段 ", notes = "删除 菜单列表字段 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "codes", value = "codes", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @DeleteMapping(value = "")
    public Result batchdel(@RequestParam List<String> codes, HttpServletRequest request) {
        try {
            iResourceColService.remove(new QueryWrapper<ResourceCol>().lambda().in(ResourceCol::getCode,codes));
            return Result.ok("操作成功！");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

 }
